

function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }



chrome.runtime.onMessage.addListener(async (msg, sender) => {
    console.log(" searchSamePic ")
    console.log(msg)
    searchSamePic(msg);

});


function searchSamePic(page){

    var keyword = getQueryString("keywords");

     $.ajax({
        url: 'https://search.1688.com/service/marketOfferResultViewService?keywords='+keyword+'&spm=a26352.13672862.searchbox.input&beginPage='+page+'&async=true&asyncCount=60&pageSize=60&startIndex=0&pageName=major&&requestId=mD6sGtCDhJxsysdMbhAeZJSWQyWN5yZfGwn1689592639379&sessionId=bbcf0d8ad9fd4845bc4c922a5a71f81d',
    　　　　 type: 'GET',
        xhrFields: {
            withCredentials: true // 这里设置了withCredentials
        },
        success: function(data) {
            var dataObject = JSON.parse(data);
            console.log(dataObject);
            var body = dataObject['data']['data']['offerList'];
            var type= "get1688forsearch";
            chrome.runtime.sendMessage({ data: {type,body,page,keyword}, close: false });
            //searchImgByApi(dataObject['data']['data']['offerList'],0);
           //searchSamePic(page+1);
        },
        error: function(err) {
            console.error(err)
            searchSamePic(page);
        }
    });
}

if(getQueryString("auto_get")=='true'){
     setTimeout(() => {
        searchSamePic(1);
     }, 3000);
}


